package cn.com.dcsgo.config;

import cn.com.dcsgo.util.jwt.JwtHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger2配置类
 *
 * @author Dcsgo
 * @since 2025/3/17 20:29
 */
@Configuration
@EnableSwagger2WebMvc
public class Swagger2Config {
    private List<Parameter> getPars() {
        List<Parameter> pars = new ArrayList<>();
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name("token")
                .description("用户token")
                .defaultValue(JwtHelper.createToken(28L, "微信用户"))
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
        pars.add(tokenPar.build());
        return pars;
    }

    @Bean
    public Docket rpcApiConfig() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("rpcApi")
                .apiInfo(rpcApiInfo())
                .select()
                //只显示api路径下的页面
                .apis(RequestHandlerSelectors.basePackage("cn.com.dcsgo"))
                .paths(PathSelectors.regex(".*/api/.*"))
                .build()
                .globalOperationParameters(getPars());
    }

    private ApiInfo rpcApiInfo() {
        return new ApiInfoBuilder()
                .title("RPC-API文档")
                .description("本文档描述了悠悠优选服务间调用接口定义")
                .version("1.0")
                .contact(new Contact("Dcsgo", "https://dcsgo.com.cn", "dcsgomail@163.com"))
                .build();
    }

    @Bean
    public Docket adminApiConfig() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("adminApi")
                .apiInfo(adminApiInfo())
                .select()
                //只显示admin路径下的页面
                .apis(RequestHandlerSelectors.basePackage("cn.com.dcsgo"))
                .paths(PathSelectors.regex("/admin/.*"))
                .build()
                .globalOperationParameters(getPars());
    }

    private ApiInfo adminApiInfo() {
        return new ApiInfoBuilder()
                .title("后台管理系统-API文档")
                .description("本文档描述了悠悠优选后台系统后端接口定义")
                .version("1.0")
                .contact(new Contact("Dcsgo", "https://dcsgo.com.cn", "dcsgomail@163.com"))
                .build();
    }

    @Bean
    public Docket weixinApiConfig() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("weixinApi")
                .apiInfo(weixinApiInfo())
                .select()
                //只显示admin路径下的页面
                .apis(RequestHandlerSelectors.basePackage("cn.com.dcsgo"))
                .paths(PathSelectors.regex("/weixin/.*"))
                .build()
                .globalOperationParameters(getPars());
    }

    private ApiInfo weixinApiInfo() {
        return new ApiInfoBuilder()
                .title("微信小程序-API文档")
                .description("本文档描述了悠悠优选微信小程序后端接口定义")
                .version("1.0")
                .contact(new Contact("Dcsgo", "https://dcsgo.com.cn", "dcsgomail@163.com"))
                .build();
    }
}
